package com.intellekt.ecert.users.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.intellekt.ecert.domain.CatalogoVO;
import com.intellekt.ecert.domain.UsuarioVO;
import com.intellekt.ecert.users.dao.UsersWSDao;
import com.intellekt.ecert.users.vo.ItemVO;

@Service
public class UsersServiceImpl implements UsersService {

	private static final Logger log = Logger.getLogger(UsersServiceImpl.class);

	@Autowired
	private UsersWSDao dao;

	@Override
	public List<CatalogoVO> getRoles() {
		return dao.getRoles();
	}

	@Override
	public List<CatalogoVO> getCountries() {
		return dao.getPaises();
	}

	@Override
	public List<CatalogoVO> getTransactions() {
		return dao.getTransacciones();
	}

	@Override
	public List<UsuarioVO> getUsers() {
		return dao.getUsers();
	}

	@Override
	public boolean userExist(String username) {
		return dao.userExist(username);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void saveUser(List<UsuarioVO> lista) {
		for (UsuarioVO vo : lista) {

			if (dao.userExist(vo.getUsername())) {
				if (vo.isModificado())
					dao.updUser(vo);
				if (vo.isEliminado())
					dao.delUser(vo);
			} else {
				dao.insUser(vo);
			}

		}
	}

	@Override
	public List<ItemVO> getItemsByTransactionId(String idTipoCertificado, String username) {

		if (idTipoCertificado.equals("F")) {
			List<ItemVO> listmer = new ArrayList<ItemVO>();
			List<ItemVO> listvu = dao.getItemsPhytoVU();
			List<ItemVO> list = dao.getItemsPhyto(idTipoCertificado, username);

			
			Map<Integer, ItemVO> map = new TreeMap<Integer, ItemVO>();
			for(ItemVO temp : listvu){
				map.put(new Integer(temp.getId()), temp);
			}
			
			for(ItemVO temp : list){
				map.remove(new Integer(temp.getId()));
			}
			
			listmer.addAll(map.values());
			
			
			return listmer;

		} else if (idTipoCertificado.equals("Z")) {
			List<ItemVO> listvu = dao.getItemsZooVU();

			return listvu;
		}
		return null;
	}

	@Override
	public List<ItemVO> getItemsById(String idMercancia, String username) {
		if (log.isDebugEnabled()) {
			log.debug("getItemsById..." + username + " " + idMercancia);
		}
		ItemVO vo = new ItemVO();
		vo.setUsername(username);
		vo.setId(idMercancia);
		/*if (dao.itemExist(vo)) {
			log.debug("no existe...");
			return null;
		} else {*/
			//log.debug("existe...");
			return dao.getItemsById(idMercancia);
		//}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void saveItems(List<ItemVO> lista) {
		for (ItemVO vo : lista) {
			log.debug("guardarMercancias" + vo.toString());
			if (dao.itemExist(vo) && vo.isEliminado()) {
				dao.delItem(vo);
			} else if (!dao.itemExist(vo) | vo.isNuevo()) {
				dao.insItem(vo);
			}

		}
	}

	@Override
	public List<ItemVO> getItemsByUser(String username) {
		
		return dao.getItemsByUser(username);
		
	}

}
